//入口函数
$(function () {
    //1.渲染用户信息
    getUserInfo();
    $('#logout').on('click', function () {
        layer.confirm('确认退出登录吗？', {icon: 3, title:'提示'}, function(index){
            //do something

            //调到登录页面，销毁token
            location.href = '/login.html';
            localStorage.removeItem('token')
            layer.close(index);
          });
    })
})

//用户信息渲染头像封装成函数
function getUserInfo() {
    axios({
        method: 'GET',
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem('token')
        // }
    }).then(res => {
        //判断
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }
        //渲染
        renderAvatar(res.data.data)
    })
}

//渲染用户信息头像
function renderAvatar(user) {
    let name = user.nickname || user.username;
    $('.welcome').html(name);
    if (user.user_pic !== null) {
        //如果有图片头像渲染图片头像---隐藏文字头像
        $('.avatar-text').hide();
        $('.layui-nav-img').show().attr('src', user.user_pic);

    } else {
        $('.layui-nav-img').hide();
        let first = name[0].toUpperCase();
        $('.avatar-text').show().html(first)
    }
 }